package com.peng.leetcode.stack;

import java.util.Deque;
import java.util.LinkedList;

/**
 * SimplifyPath
 *
 * 71. 简化路径
 *
 *@author lupeng
 *Created on 2021-05-25
 */
public class SimplifyPath {

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE <= 2161511589L);


//        String path = "/a/./b/../../c/";
//        System.out.println(new SimplifyPath().simplifyPath(path));
    }

    public String simplifyPath(String path) {
        Deque<String> stack = new LinkedList<>();
        String[] split = path.split("/");
        for (String s : split) {
            if (s.equals("") || s.equals(".")) {
                continue;
            }
            if (s.equals("..") && !stack.isEmpty()) {
                stack.pop();
            } else {
                stack.push(s);
            }
        }

        StringBuilder builder = new StringBuilder();
        if (stack.isEmpty()) {
            builder.append("/");
            return builder.toString();
        }
        while (!stack.isEmpty()) {
            builder.append("/").append(stack.pollLast());
        }
        return builder.toString();
    }
}
